var allowSub = true;
var errorMsg;

$(function(){
	layui.use(['form', 'layedit', 'laydate'],function(){
		 var form = layui.form
		  ,layer = layui.layer
		  ,layedit = layui.layedit
		  ,laydate = layui.laydate;
		 
		 laydate.render({
			 elem: '#date'
			 ,trigger: 'click'
		 });
		 
		 $("#login_id").blur(function(){
			var formData = form.val('user-form');
			
			var login_id = formData.login_id;
			
			if(isNull(login_id)){
				return;
			}
			
			if('super'===login_id){
				layer.msg('此账号为超级管理员账号,不可注册',{icon:7});
				errorMsg = '此账号为超级管理员账号,不可注册';
				allowSub = false;
				return;
			}
			
			$.ajax({
				type : 'post'
				,dataType : 'json'
				,url : access_url+'/system/systemmanage/queryloginlist'
				,contentType : 'application/json;charset=UTF-8'
				,data : JSON.stringify({
					pageNum : 1
					,pageSize : 1
					,params : {
						login_id : login_id
					}
				})
				,success : function(data){
					var checkLogin = data.retdata.pageData;
					if(!isNull(checkLogin)){
						errorMsg = '此账号已有人使用';
						
						layer.msg(errorMsg,{icon:7});

						allowSub = false;
						return;
					}
					layer.msg('账号可以使用',{icon:1});
					allowSub = true;
				}
			})
		 })
		 
		 $('#rewrite_password').blur(function(){
			 var formData = form.val('user-form');
			 var login_password = formData.login_password;
			 var rewrite_password = formData.rewrite_password;
			 
			 if(isNull(login_password)||isNull(rewrite_password)){
				 return;
			 }
			 
			 if(login_password!=rewrite_password){
				 errorMsg = '两次输入的密码不一致';
					
				layer.msg(errorMsg,{icon:7});

				allowSub = false;
				return;
			 }
			 allowSub = true;
		 })
		 
		 $('#test-get-data').click(function(){
			 if(!allowSub){
				 layer.msg(errorMsg, {icon: 5});
				 return;
			 }
			 
			 var formData = form.val('user-form');
			 console.log(JSON.stringify(formData));
			 
			 var login_id = formData.login_id;
			 var person_name = formData.person_name;
			 var login_password = formData.login_password;
			 var rewrite_password = formData.rewrite_password;
			 var phone_no = formData.phone_no;
			 var email = formData.email;
			 var qqno = formData.qqno;
			 var wechatid = formData.wechatid;
			 var birth = formData.birth;
			 var orgid = $('#orgid').attr('data-value');
			 var allow_login = formData.allow_login;
			 var addr = formData.addr;
			 var roles = [];
			 $("input:checkbox[name='role']:checked").each(function(){
				 var role = `{"role_id":"${$(this).val()}"}`
				 roles.push(JSON.parse(role));
			 })
			 
			 if(isNull(login_id)){
				 layer.msg('账号不允许为空!', {icon: 5});
				 return;
			 }
			 
			 if(isNull(person_name)){
				 layer.msg('姓名不允许为空!', {icon: 5});
				 return;
			 }
			 
			 if(isNull(login_password)){
				 layer.msg('登陆密码不允许为空!', {icon: 5});
				 return;
			 }
			 
			 if(isNull(rewrite_password)){
				 layer.msg('验证密码不允许为空!', {icon: 5});
				 return;
			 }
			 
			 if(isNull(phone_no)){
				 layer.msg('手机号码不允许为空!', {icon: 5});
				 return;
			 }
			 
			 if(isNull(orgid)){
				 layer.msg('组织机构不允许为空!', {icon: 5});
				 return;
			 }
			 
			 if(isNull(roles)){
				 layer.msg('请选择用户角色!', {icon: 5});
				 return;
			 }
			 
			 if(isNull(allow_login)){
				 allow_login = 0;
			 }
			 
			 layer.load(2);
			 
			 $.ajax({
				 type : 'post'
				 ,dataType : 'json'
				 ,url : access_url+'/system/systemmanage/adduser'
				 ,contentType : 'application/json;charset=UTF-8'
				 ,data : JSON.stringify({
					 login_id : login_id
					 ,login_password : login_password
					 ,orgid : orgid
					 ,allow_login : allow_login
					 ,login_roles : roles
					 ,person : {
						 person_name : person_name
						 ,phone_no : phone_no
						 ,email : email
						 ,qqno : qqno
						 ,wechatid : wechatid
						 ,addr : addr
						 ,birth : birth
					 }
				 })
				 ,success : function(data){
					layer.closeAll('loading');
					if(data.code != 200){
						layer.msg(data.msg, {icon: 5});
						return;
					}
					parent.layer.alert(data.msg,{icon:6});
					parent.loadTable();
					parent.layer.closeAll('iframe');
				 }
			 })
		 })
	})
	
	getAllRoles();
	function getAllRoles(){
		console.log('进入获取角色方法');
		$.ajax({
			type : 'get'
			,dataType : 'json'
			,async : false
			,url : access_url+'/system/systemmanage/getallroles'
			,success : function(data){
				console.log("data",data);
				var retlist = data.retlist;
				var len = retlist.length;
				
				var roleshtml = '';
				for (var i = 0; i < len; i++) {
					var role_level = retlist[i].role_level;
					var role_id = retlist[i].role_id;
					var role_name = retlist[i].role_name;
					
					var role = `<input type="checkbox" name="role" value="${role_id}" title="${role_name}">`;
					roleshtml += role;
				}
				
				$('#roles-item').append(roleshtml);
			}
		})
	}
})

$('#orgid').click(function(){
	layer.open({
		type : 2
		,title : '选择组织机构'
		,area : ['300px','450px']
		,offset : '20px'
		,fixed : false
		,content : 'chooseOrg.html'
		,resize : false
	})
})